package org.dk;


import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.*;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Aspect
public class LogAspect {


    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * org.dk.service.*.*(..))")
    public void aopLog(){}

    //进通知 前置通知
    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable{
        //l拦截的所有方法 ,不管是什么类型的方法,返回的都是obj
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        logger.info("进入 {} 方法 , 参数为 {}",signature,args);
    }
    @AfterReturning(value = "aopLog()",returning = "obj")
    public void doAfter(Objects obj){
     logger.info("方法调用成功,获取到的返回值为: {} ", JSONObject.toJSONString(obj));
    }
}
